/**
 * Created by Administrator on 2016/7/5.
 */
/**
 * Created by Administrator on 2016/7/5.
 */
"use strict";
swan_pc
    .controller("TaskDetailController",function($scope,TaskById,$rootScope,TimeExchange,$timeout,$state,Task,Clip,ngDialog){
        $scope.TaskKey={
            status:"状态",
            name:"任务名称",
            isDelayed:"是否拖延",
            isImportant:"是否重要",
            isPrincipal:"是否属于组织",
            linkedActivityId:"link的活动id",
            linkedActivityType:"link的活动类型",
            organizationId:"组织id",
            ownerId:"UserId",
            relatedCompany:"相关公司",
            relatedContact:"相关联系人",
            relatedOpp:"相关机会",
            remark:"备注",
            startTime:"开始时间",
            endTime:"结束时间",
            type:"类型",
            who:"被邀请人"
        }
        $scope.presentTask={
            status:null,
            name:null,
            isDelayed:null,
            isImportant:null,
            isPrincipal:null,
            linkedActivityId:null,
            linkedActivityType:null,
            organizationId:null,
            ownerId:null,
            relatedCompany:null,
            relatedContact:null,
            relatedOpp:null,
            remark:null,
            startTime:null,
            endTime:null,
            type:null,
            who:null,
        }
        $scope.init=function() {
            $scope.status_array = [{"key": "DRAFT", "value": "草稿"},
                {"key": "CONFIRM", "value": "确认"}, {"key": "CANCELED", "value": "取消"},
                {"key": "CLOSED", "value": "关闭"}, {"key": "PENDING", "value": "待定"}];
        }
        $scope.init();
        $scope.init_show=function(){
            $scope.InfoShow=true;
        }
        $scope.init_show();
        $scope.show=function(index){
            //alert(x.value);
            $timeout(function(){
                var x=document.getElementById("show"+index);
                x.focus();
            });
        };
        if($rootScope.presentTaskid!=null&&$rootScope.presentTaskid!=""){
            TaskById.query({taskid:$rootScope.presentTaskid},function(result){
                $scope.presentTask=result;
                var startDate=new Date($scope.presentTask.startTime);
                var endDate=new Date($scope.presentTask.endTime);
                $scope.presentTask.startTime=startDate;
                $scope.presentTask.endTime=endDate;
                //console.log($scope.presentTask.name+$scope.presentTask.startTime+$scope.presentTask.endTime);
            })
        }
        $scope.TimeShow=function(time){
            if(time==null){
                return "";
            }
            var timer=time.toString();
            timer=TimeExchange.StringtoLocalTime(timer);
            timer=timer.split(" ");
            return timer[0];
        }
        $scope.status_show=function(option){
            if(option=="DRAFT"||option=="草稿") return "草稿";
            if(option=="CONFIRM"||option=="确认") return "确认";
            if(option=="CANCELED"||option=="取消") return "取消";
            if(option=="CLOSED"||option=="关闭") return "关闭";
            if(option=="PENDING"||option=="待定") return "待定";
        }
        $scope.judge_show=function(option){
            if(option=="1") return "是";
            if(option=="0") return "否";
        }
        $scope.EditToDatabase=function(){
            TaskById.update({},$scope.presentTask,
                function (result) {
                    swal("更新成功", "任务" + " " + $scope.presentTask.name, "success");
                    $state.go("taskcalshow");
                },
                function (result) {
                    swal("更新失败", "日程" + " " + $scope.presentTask.name, "error");
                })
        }
        $scope.BackToCal=function(){
            $state.go("taskcalshow");
        }
        $scope.DeleteFromDatabase=function(){
            swal({
                title: "确定要删除该任务吗",
                text: "删除任务会降该任务所关联的Clip全部删除！！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                cancelButtonText: "取消删除",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    //删除与该Task相关的所有Clip
                    var DeleteClipFlag=0;
                    $scope.predicate = 'id';
                    $scope.reverse = true;
                    $scope.page = 1;
                    $scope.clips=[];
                    Clip.query({page: $scope.page - 1, size: 500, sort: [$scope.predicate + ',' + ($scope.reverse ? 'asc' : 'desc'), 'id']},
                        function(result, headers) {
                            $scope.totalItems = headers('X-Total-Count');
                            $scope.clips = result;
                            console.log("result:"+result);
                            console.log("clips"+$scope.clips);
                            angular.forEach($scope.clips,function(data,index,array){
                                //console.log(data.name+ " "+data.individualTaskId+" "+$rootScope.presentTaskid);
                                //console.log($scope.clips.length+ " "+index+" "+data.individualTaskId+" "+$rootScope.presentTaskid)
                                if(data.individualTaskId==$rootScope.presentTaskid){
                                    //console.log($scope.clips.length+ " "+index+" "+data.individualTaskId+" "+$rootScope.presentTaskid)
                                    Clip.delete({clipid:data.id}, function(result){
                                        //console.log("clip delete:"+data.name+"deleted");
                                    }, function(result){
                                        //console.log("clip delete"+data.name+"can't delete");
                                        DeleteClipFlag=1;
                                    });
                                }
                                if(index==($scope.clips.length-1)&&(DeleteClipFlag==0)){
                                    //console.log("task delete run");
                                    TaskById.delete({taskid: $rootScope.presentTaskid},
                                        function (result) {
                                            swal("删除成功", $scope.presentTask.name, "success");
                                            $state.go("taskcalshow");
                                        },
                                        function (result) {
                                            swal("删除失败", $scope.presentTask.name, "error");
                                        })
                                }
                            })
                        });
                }else
                {     swal("删除取消",$scope.presentTask.name,"error");
                }
            });
        }
        $scope.display=function(option){
            if(option==0){
                $scope.InfoShow=false;
            }
            if(option==1){
                $scope.InfoShow=true;
            }
        }
        $scope.SelectForContact=function(){
            $rootScope.PresentDialogContactIdName={};
            $scope.singleResult={};
            //console.log("in:addclip.relatedContact::"+$scope.addclip.relatedContact);
            if ($scope.presentTask.relatedContact != null && $scope.presentTask.relatedContact != "") {
                console.log("Contact FuZhi is run");
                var contact = $scope.presentTask.relatedContact;
                contact = contact.split(",");
                for (var i = 0; i < contact.length; i++) {
                    var contactIdName = contact[i].split(":");
                    console.log("contactIdName"+contactIdName);
                    $rootScope.PresentDialogContactIdName[contactIdName[0]] = contactIdName[1];
                }
            }
            //console.log("in:PresentContactIdName:::::"+$scope.PresentContactIdName);
            ngDialog.open({
                templateUrl:"scripts/components/myself/calendar/dialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.CloseContactDialog=function(){
                        $rootScope.PresentDialogContactIdNameString=$rootScope.PresentDialogContactIdName.toString();
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        $scope.SelectForCompany=function(){
            $rootScope.PresentDialogCompanyIdName={};
            //console.log("in:addclip.relatedContact::"+$scope.addclip.relatedContact);
            if ($scope.presentTask.relatedCompany != null && $scope.presentTask.relatedCompany != "") {
                console.log("Company FuZhi is run");
                var company = $scope.presentTask.relatedCompany;
                company = company.split(",");
                for (var i = 0; i < company.length; i++) {
                    var companyIdName = company[i].split(":");
                    console.log("companyIdName"+companyIdName);
                    $rootScope.PresentDialogCompanyIdName[companyIdName[0]] = companyIdName[1];
                }
            }
            //console.log("in:PresentContactIdName:::::"+$scope.PresentContactIdName);
            ngDialog.open({
                templateUrl:"scripts/components/myself/calendar/companydialog.html",
                controller:["$scope",function($scope){
                    $scope.CloseCompanyDialog=function(){
                        $rootScope.PresentDialogCompanyIdNameString=$rootScope.PresentDialogCompanyIdName.toString();
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        $scope.SelectForOpp=function(){
            $rootScope.PresentDialogOppIdName={};
            $scope.singleResult={};
            //console.log("in:addclip.relatedCompany::"+$scope.addclip.relatedCompany);
            if ($scope.presentTask.relatedOpp != null && $scope.presentTask.relatedOpp != "") {
                console.log("Opp FuZhi is run");
                var opp = $scope.presentTask.relatedOpp;
                opp = opp.split(",");
                for (var i = 0; i < opp.length; i++) {
                    var oppIdName = opp[i].split(":");
                    console.log("OppIdName"+oppIdName);
                    $rootScope.PresentDialogOppIdName[oppIdName[0]] = oppIdName[1];
                }
            }
            //console.log("in:PresentContactIdName:::::"+$scope.PresentContactIdName);
            ngDialog.open({
                templateUrl:"scripts/components/myself/calendar/oppdialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.CloseCompanyDialog=function(){
                        $rootScope.PresentDialogOppIdNameString=$rootScope.PresentDialogOppIdName.toString();
                        //console.log("OppResult:"+$rootScope.PresentDialogCompanyIdName);
                        //console.log("OppString:"+$rootScope.PresentDialogCompanyIdNameString);
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        $scope.relatedOppInfoShow=function(){
            if($rootScope.PresentDialogOppIdNameString!=""&&$rootScope.PresentDialogOppIdNameString!=undefined){
                $scope.presentTask.relatedOpp=$rootScope.PresentDialogOppIdNameString;
                //console.log("in:"+$scope.addclip.relatedContact);
                var OppShow=$scope.ContactCompanyInfoSplit($scope.presentTask.relatedOpp);
                return OppShow;
            }else{
                var OppShow=$scope.ContactCompanyInfoSplit($scope.presentTask.relatedOpp);
                return OppShow;
            }
        }
        $scope.relatedCompanyInfoShow=function(){
            if($rootScope.PresentDialogCompanyIdNameString!="" &&$rootScope.PresentDialogCompanyIdNameString!=undefined){
                $scope.presentTask.relatedCompany=$rootScope.PresentDialogCompanyIdNameString;
                //console.log("in:"+$scope.addclip.relatedContact);
                var CompanyShow=$scope.ContactCompanyInfoSplit($scope.presentTask.relatedCompany);
                return CompanyShow;
            }else{
                var CompanyShow=$scope.ContactCompanyInfoSplit($scope.presentTask.relatedCompany);
                return CompanyShow;
            }
        }
        $scope.relatedContactInfoShow=function(){
            if($rootScope.PresentDialogContactIdNameString!="" && $rootScope.PresentDialogContactIdNameString!=undefined){
                $scope.presentTask.relatedContact=$rootScope.PresentDialogContactIdNameString;
                //console.log("in:"+$scope.addclip.relatedContact);
                var ContactShow=$scope.ContactCompanyInfoSplit($scope.presentTask.relatedContact);
                return ContactShow;
            }else{
                var ContactShow=$scope.ContactCompanyInfoSplit($scope.presentTask.relatedContact);
                return ContactShow;
            }
        }
        $scope.ContactCompanyInfoSplit=function(str){
            //console.log("str:"+str);
            if(str=="" || str==null){
                return str;
            }else{
                var str_res="";
                var str_new=str.split(",");
                for(var i=0;i<str_new.length;i++){
                    var strRes=str_new[i].split(":");
                    //console.log("strRes:"+strRes);
                    str_res=str_res+strRes[1]+","
                    //console.log("str_res"+str_res);;
                }
                var length=str_res.length-1;
                var resultString=str_res.substring(0,length);
                //console.log("resultString:"+resultString);
                return resultString;
            }
        }
    })
